﻿using MediatR;
using Microsoft.Data.SqlClient;
using NPOI.SS.UserModel;
using S.Authorization.Common;
using S.Authorization.Common.Dto;
using S.Authorization.Domain.Model.Authority;
using S.Authorization.Domain.Model.Organization;
using S.Authorization.Infrastructure;
using S.Authorization.Infrastructure.Interface.SheepFarms;
using S.Authorization.Read.API.Application.Command.SheepFarmsCommand;

namespace S.Authorization.Read.API.Application.CommandHandlers.SheepFarmsHandler
{
    /// <summary>
    /// 羊场显示
    /// </summary>
    public class SheepFarmsGetAllCommandHandler : IRequestHandler<SheepFarmsGetAllCommand, Pager<SheepFarmsModel>>, IRequestHandler<SheepFarmsSelectCommand, List<SelectDto>>,IRequestHandler<SheepFarmsByIdCommand,List<SheepFarmsModel>>
    {
        private readonly ISheepFarmsRepository repository;
        private readonly DbHelper dbHelper;

        public SheepFarmsGetAllCommandHandler(ISheepFarmsRepository repository,DbHelper _dbHelper)
        {
            this.repository = repository;
            this.dbHelper = _dbHelper;
        }

        public async Task<Pager<SheepFarmsModel>> Handle(SheepFarmsGetAllCommand request, CancellationToken cancellationToken)
        {
            List<SheepFarmsModel> SheepList = await repository.GetAllAsync();
            if (!string.IsNullOrEmpty(request.SheepFarmsFullName))
            {
                SheepList = SheepList.Where(x => x.SheepFarmsFullName.Contains(request.SheepFarmsFullName)).ToList();
            }
            if (!string.IsNullOrEmpty(request.Responsible))
            {
                SheepList = SheepList.Where(x => x.Responsible.Contains(request.Responsible)).ToList();
            }
            int totalCount = SheepList.Count();
            int pageCount = (int)Math.Ceiling(totalCount * 1.0 / request.PageSize);
            SheepList = SheepList.OrderBy(x => x.SheepFarmsId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();

            return new Pager<SheepFarmsModel>()
            {
                PageIndex=request.PageIndex,
                PageSize=request.PageSize,
                TotalCount = totalCount,
                PageCount = pageCount,
                resultData = SheepList
            };
        }
        /// <summary>
        /// 羊场下拉框
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<List<SelectDto>> Handle(SheepFarmsSelectCommand request, CancellationToken cancellationToken)
        {
            List<SheepFarmsModel> list = repository.GetAllAsync().Result;
            List<SelectDto> children = list.Select(x => new SelectDto
            {
                value = x.SheepFarmsId,
                label = x.SheepFarmsFullName,
            }).ToList();
            return children;
        }
        /// <summary>
        /// 羊场返填
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<List<SheepFarmsModel>> Handle(SheepFarmsByIdCommand request, CancellationToken cancellationToken)
        {
            string sql = "SELECT * FROM [dbo].[SheepFarmsModel] WHERE SheepFarmsId=@SheepFarmsId";
            SqlParameter[] value =
            {
                new SqlParameter("@SheepFarmsId",request.SheepFarmsId),
            };
            var list = await dbHelper.GetListDataBySql<SheepFarmsModel>(sql, value);
            return list;
        }


    }
}
